<?php
namespace app\wechat\controller;
use think\Controller;
use app\common\model\China;
use app\common\model\Wechat as WModel;
Vendor('Wechat.Wechat');
class Index extends Controller{

  /**
   * 微信配网
   * @return [type] [description]
   */
  public function netWork(){
    
      //判断是否是微信访问
      $user_agent = $_SERVER['HTTP_USER_AGENT'];
      if (strpos($user_agent, 'MicroMessenger') === false) {
          exit;
      }

      $options["appid"] = config("WECHAT_APPID");
      $options["appsecret"] = config("WECHAT_APPSERVER");
      $weObj = new \Org\Util\Wechat($options);

      $url = "http://ferr.mydehui.com/Home/Index/netWork";

      $dataInfo = $weObj->getJsSign($url);

      $this->assign("dataInfo",$dataInfo);
      return view();
  }
  /**
   * 省市区列表
   * @return [type] [description]
   */
  public function areas(){
      if(request()->isAjax()){
          $chinaModel = new China();
          $pid = input('post.pid',0);
          $areasList = $chinaModel->getList($pid);

          show(1,'',$areasList);
      }
  }
  
  /**
   * 微信开发
   */
  public function wetChat(){

    $options["token"] = config('WECTAT_TOKEN');
    ob_clean();

    $weObj = new \Wechat($options);
    $weObj->valid();

    $weModel = $weObj->getRev();
    //类型
    $type = $weModel->getRevType();
    //内容
    $content = $weModel->getRevContent();
    $help = "若无法解决您的问题请联系管理员";


    switch($type) {
      case $weObj::MSGTYPE_TEXT:
          //先全部转换成小写
          $content = strtolower($content);
          //将所有空格替换成1个空格
          $con = preg_replace("/\s(?=\s)/","\\1",$content);
          $con = explode(" ",$con);

          switch ($con[0])
                {
                  case 'help':
                      $weObj->text($help)->reply();
                      break;
                  default :
                      $weObj->text($help)->reply();
                      break;
                }
          break;
      case $weObj::MSGTYPE_EVENT:
          $event = $weModel->getRevEvent();
          switch ($event["event"])
            {
              case $weObj::EVENT_SUBSCRIBE :
                  //关注
                 $contentStr = "感谢您关注江苏福瑞至物联网";

                 $openid = $weModel->getRevFrom();
                 $WModel = new WModel();
                 $WModel->follows($openid);

                 $weObj->text($contentStr)->reply();
                 break;
              case $weObj::EVENT_SCAN:

                 $weObj->text($contentStr)->reply();
                 break;
              case $weObj::EVENT_MENU_CLICK:
                 switch ($event["key"]) {
                   case 'rselfmenu_1_2':
                       //帮助
                       $contentStr = $help;
                       break;
                   default:
                       $contentStr = "操作错误";
                       break;
                 }
                 $weObj->text($contentStr)->reply();
                 break;
              default :
                  //取消关注
                 break;
            }
          break;
      case $weObj::MSGTYPE_IMAGE:
         break;
         default:
      $weObj->text($help)->reply();
    }
    exit;
  }

  //微信设置
  public function wetChatsetup(){
        exit;
       $options["token"] = "123456";
       $options["appid"] = config("WECHAT_APPID");
       $options["appsecret"] = config("WECHAT_APPSERVER");
       $weObj = new \Wechat($options);
       
       //手动添加设备
       $url_1 = "http://ferr.mydehui.com/wechat/Cons/adds";
       // 设备列表
       $url_2 = "http://ferr.mydehui.com/wechat/Cons/devs";
       // 个人中心
       $url_3 = "http://ferr.mydehui.com/wechat/Cons/infos";
       //产品订单提交
       $order_1 = "http://ferr.mydehui.com/wechat/Cons/aorder";
       //项目订单提交
       $order_2 = 'http://ferr.mydehui.com/wechat/Cons/addpro';

       $a_1 = $weObj->getOauthRedirect($url_1,"","snsapi_base");
       $a_2 = $weObj->getOauthRedirect($url_2,"","snsapi_base");
       $a_3 = $weObj->getOauthRedirect($url_3,"","snsapi_base");
       $o_1 = $weObj->getOauthRedirect($order_1,"","snsapi_base");
       $o_2 = $weObj->getOauthRedirect($order_2,"","snsapi_base");


       // $a = $weObj->getMenu();
       // v_dump($a);exit;
       
       // echo $a_1.'<br>';
       // echo $a_2.'<br>';
       // echo $a_3.'<br>';
       // exit;

        // $wetchatModel = new \Common\Helper\Weixin();

        // $openid = "ojM70wGg15zCctbLDQIJku3uwKes";
        // $dev_id = "123456";
        // $dev_name = "赵磊军";

        // $a = $wetchatModel->sends($openid,$dev_id,$dev_name,3);
        // var_dump($a);exit;

        // // 微信登录
        //$options["appid"] = C("WECHAT_PC_APPID");
        //$options["appsecret"] = C("WECHAT_PC_APPSERVER");

        //$weObj = new \Org\Util\Wechat($options);
        //$url = "http://www.swaylink.cn/swaylink.php/King/User/wchatB";
  
        // $url = "http://www.mydehui.com/swaylink.php/King/User/weixinBang";
        // $url = "http://www.mydehui.com/swaylink.php/King/User/wchatB";
        //$a = $weObj->getOauthPCRedirect($url,"","snsapi_login");
        //var_dump($a);exit;

          $data =  array (
             'button' => array (
                  0 => array (
                      'name' => '设备',
                      'sub_button' => array (
                          0 => array (
                              'type' => 'scancode_push',
                              'name' => '扫码添加',
                              'key' => 'rselfmenu_0_1',
                          ),
                          1 => array(
                              'type' => 'view',
                              'name' => '手动添加',
                              'url'  => $a_1,
                          ),
                          2 => array(
                              'type' => 'view',
                              'name' => '查看设备',
                              'url'  => $a_2,
                          ),
                      )
                  ),
                  1 => array(
                      'name' => '订单业务',
                      'sub_button' => array (
                          0 => array (
                              'type' => 'view',
                              'name' => '产品订单提交',
                              'url' => $o_1,
                          ),
                          1 => array (
                              'type' => 'view',
                              'name' => '项目订单提交',
                              'url' => $o_2,
                          ),
                      )
                  ),
                  2 => array (
                    'name' => '我',
                    'type' => 'view',
                    'url' => $a_3,
                  )
             ),
          );
  
          $return = $weObj->createMenu($data);
          var_dump($return);exit;
       // $weixin = new \Common\Helper\Weixin();
        // $return = $weixin->wechat_send("abkDyj");
        // $data = "101.200.216.84/swaylink.php/Home/Pay/index";
        // $return = $weObj->getOauthRedirect($data,1);

      // $data = array("0000qz","0000r0","0000r1","0000r2","0000r3","0000r4","0000r5","0000r6","0000r7","0000r8","0000r9","0000ra","0000rb","0000rc","0000rd",);
      // foreach ($data as $value) {
      //        $weixin->createCode($value);
      //        $return = $weixin->downFromWeiXin($value);
      // }



       //    $return = $weObj->checkAuth();
       // $return = $weObj->deleteMenu();

       //  $return = $weixin->wechat_send("000034");

       // $weModel = new \Common\Helper\Weixin();
       // $weModel->abcd();
      // v_dump($return);
  }
}